package com.example.administrator.jingpingou.ui;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.example.administrator.jingpingou.R;
import com.example.administrator.jingpingou.fragment.MineFragment;
import com.example.administrator.jingpingou.fragment.ShopFragment;
import com.example.administrator.jingpingou.fragment.TeJiaFragment;
import com.example.administrator.jingpingou.fragment.XianshiFragment;
import com.example.administrator.jingpingou.fragment.ZhuTiFragment;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;

public class MainActivity extends AppCompatActivity implements PlatformActionListener {
    private RadioGroup mRadioGroup;
    private Fragment mCurrentFragment;
    private FragmentManager mManager;
    private ImageView mImageViewContent,mImageViewFinish;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iniView();
        iniData();
        setListener();
    }

    private void iniData() {
        mManager=getSupportFragmentManager();
        showAndHide(R.id.fl_main_content, ShopFragment.class);

    }



    private void setListener() {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Class<? extends Fragment> clazz=null;
                switch (checkedId) {
                    case  R.id.rb_main_shop:
                        clazz=ShopFragment.class;
                        break;
                    case R.id.rb_main_tejia:
                        clazz= TeJiaFragment.class;
                        break;
                    case R.id.rb_main_xianshi:
                        clazz= XianshiFragment.class;
                        break;
                    case R.id.rb_main_zhuti:
                        clazz= ZhuTiFragment.class;
                        break;
                    case R.id.rb_main_mine:
                        clazz= MineFragment.class;
                        break;
                }
                showAndHide(R.id.fl_main_content,clazz);
            }
        });
        mImageViewFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mImageViewContent.setVisibility(View.GONE);
                mImageViewFinish.setImageResource(R.mipmap.tab_adb_closed_s);
                mImageViewFinish.setVisibility(View.GONE);
            }
        });
    }

    private void iniView() {
        mRadioGroup= (RadioGroup) findViewById(R.id.rg_main_index);
        mImageViewContent= (ImageView) findViewById(R.id.iv_main_content);
        mImageViewFinish= (ImageView) findViewById(R.id.iv_main_finish);
    }
    private void showAndHide(int contentId, Class<? extends Fragment> clazz){
        //判断当前的fragment与需要替换的fragment是不是同一个
        if(mCurrentFragment!=null&&mCurrentFragment.getClass().getSimpleName().equals(clazz.getCanonicalName())) {
            return;
        }
        FragmentTransaction transaction = mManager.beginTransaction();
        Fragment fragment=null;
        try {

            //判断fragment有没有添加过
            Fragment fragmentByTag = mManager.findFragmentByTag(clazz.getSimpleName());
            if(fragmentByTag!=null) {
                //显示需要显示的fragment
                transaction.show(fragmentByTag);
                //隐藏当前的fragment
                transaction.hide(mCurrentFragment);
                //记录当前显示我fragment
                mCurrentFragment=fragmentByTag;
            }else{
                //通过无参的公开的构造函数来创建fragment实例
                fragment=clazz.newInstance();
                //当前的fragment没有添加过
                //把fragment添加到fragmentmanager里面
                transaction.add(contentId,fragment,clazz.getSimpleName());
                if(mCurrentFragment!=null) {
                    //隐藏当前的fragment
                    transaction.hide(mCurrentFragment);

                }
                //记录当前的fragment是哪个
                mCurrentFragment=fragment;
            }
            transaction.commit();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {

    }

    @Override
    public void onCancel(Platform platform, int i) {
        finish();
    }
}
